class Solution {
public:
  ListNode *detectCycle(ListNode *head) {

    ListNode *slow = head;
    ListNode *fast = head;
    bool exitCycle = false;

    while(fast && fast->next){
      slow = slow->next;
      fast = fast->next->next;
      if(slow == fast){
        exitCycle = true;
        break;
      }
    }

    if(exitCycle){
      fast = head;
      while(fast != slow){
        fast = fast->next;
        slow = slow->next;
      }
      return fast;
    }

    return nullptr;

  }
};